/*
 *   Copyright (C) 2009  Jens Kieselbach
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You may obtain a copy of the License at http://www.gnu.org/licenses/gpl.html
 */

package de.kieselbach.swing

import javax.swing.JButton
import javax.swing.UIManager

public class IconToggleButton extends JButton {

    def iSelectedIcon
    def iUnselectedIcon

    def selectionChanged;

    public IconToggleButton(def selectedicon, def unselectedicon, def initsel) {

        super()

        iSelectedIcon = selectedicon
        iUnselectedIcon = unselectedicon
        selectedIcon = selectedicon
        icon = unselectedicon

        rolloverSelectedIcon = selectedicon
        rolloverIcon = unselectedicon

        selected = initsel
        icon = initsel ? iSelectedIcon : iUnselectedIcon

        def lnf = UIManager.getLookAndFeel()
        disabledSelectedIcon = lnf.getDisabledIcon(this, selectedicon)
        disabledIcon = lnf.getDisabledIcon(this, unselectedicon)

        actionPerformed = {event ->

            if (selected) {
                selected = false
                icon = iUnselectedIcon
            }
            else {
                selected = true
                icon = iSelectedIcon
            }

            if (selectionChanged != null) {
                selectionChanged(event)
            }
        }
    }
}
